package com.kpx.app.base;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.kpx.app.configs.BroadcastFilters;

/**
 * Created by Administrator on 2017/4/5.
 */

public abstract class BaseService extends Service {

    /**
     * 广播过滤器
     */
    private IntentFilter intentFilter;
    /**
     * 广播接收
     */
    private BroadcastReceiver receiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        registReceiver();
    }

    /**
     * 广播注册
     */
    private void registReceiver() {
        setAction();
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                onReceiver(context, intent);
            }
        };
        registerReceiver(receiver, intentFilter);
    }

    /**
     * 广播处理
     */
    public abstract void onReceiver(Context context, Intent intent);

    /**
     * 设置广播接收数据（如果有广播需要处理，需要在这里进行配置）
     */
    private void setAction() {
        intentFilter = new IntentFilter();
        BroadcastFilters.addAction(intentFilter);
    }
}
